#include<iostream>
#include<map>
#include<unordered_map>
#include<string>
#include<vector>
#include<fstream>
#include<sstream>

using std::cout;
using std::cin;
using std::endl;
using std::unordered_map;
using std::string;
using std::vector;
using std::ifstream;
using std::istringstream;

class Conver
{
public:
    void print(vector<string> lhs,std::ostream& rhs)
    {
        for(auto& i: lhs)
        {
            rhs<<i<<" ";
        }
        rhs<<endl;
    }
    void read(const string& map_name, const string& file_name)
    {
        ifstream ifs_map(map_name);

        string line_map;

        int file_num=0;
        //拿到每行数据并处理

        while(std::getline(ifs_map,line_map))
        {
            //读到每行数据分割
            string word;
            istringstream iss(line_map);
            iss>>word;
            line_map.erase(line_map.begin(),word.size()+line_map.begin()+1);
            _map[word]=line_map;
        }
        ifstream ifs_file(file_name);
        string line_file;

        while(std::getline(ifs_file,line_file))
        {
            //读到每行数据分割
            ++file_num;
            string word;
            istringstream iss(line_file);
            while(iss>>word)
            {
                _file[file_num].push_back(word);
            }
            print(_file[file_num],cout);
        }
        ifs_file.close();
        //拿到所有数据并存入之后
    }
    void change(const string & filename)
    {
        cout<<"888888888888888<<endl;"<<endl;
        //修改map里面的值
        for(auto& i: _file)
        {
            //遍历每行的每个单词
            for(auto& j: i.second)
            {
                if(_map.count(j))
                {
                    j=_map[j];
                    cout<<j<<endl;
                }
            }
        }
        cout<<"888888888888888<<endl;"<<endl;
        //map(file)里面的成员已经修改
        //文件写入
        std::ofstream ofs(filename,std::ios::app);
        //测试是否改变不改变源文件

        for(auto& i:_file)
        {
            print(i.second,ofs);
        }
        ofs.close();
    }



private:

    unordered_map<string,string> _map;
    std::map<int,vector<string>> _file;

};



int main(void)
{
    Conver cv;
    cout<<"请输入文件名一"<<endl;
    string map_name;
    cin>>map_name;
    cout<<"请输入文件名二"<<endl;
    string file_name;
    cin>>file_name;

    cv.read(map_name,file_name);
    cv.change(file_name);

}





